<?php
require_once '../classes/ConnectionDB.php';
require_once '../classes/UtenteManager.php';
require_once '../classes/DisciplinaManager.php';
require_once '../classes/DisciplinaDataManager.php';
require_once '../classes/TestDataManager.php';
require_once '../bean/Argomento.php';
$connection = ConnectionDB::getInstance()->connectToDB();
$codiceUtente = $_GET['codiceUtente'];
$result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

$argomenti = TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);
$disciplineUtente = DisciplinaDataManager::getInstance()->getDisciplineUtente($connection);

$argomentiFromQuestion = TestDataManager::getInstance()->getQuestionArguments($connection);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="../bases/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="../bases/jquery-ui-1.7.2.custom.min.js"></script>
    <title>Nuovo Argomento</title>
    <style type="text/css">
      @import url("../css/template.css");
      @import url("../css/jquery-custom.css");
      a#viewcss{color: #00f;font-weight: bold}

      #tableRes td{
        text-align: center;
      }
      #tableRes th{
        background-color: gray;
        text-align: center;
      }
    </style>

    <script type="text/javascript">
      var i=0;
      var listaStudenti;
      var numItem=6;
      var currentIndex=0;
      var rimanenti = 100;

      window.onload
      {
        listaStudenti = new Array();
<?
for ($j = 0; $j < count($argomenti); $j++) {
?>

      listaStudenti[i] = "<?= $argomenti[$j]->getCodice() ?>*<?= $argomenti[$j]->getNome() ?>*<?= $argomenti[$j]->getDescrizione() ?>";
      i++;
<? } ?>

  }
  function manageNav(ind)
  {
    var tmp= new Array();

    var str="<table id='tableRes' width='100%'>"
      +"<tr>"
      +"<th>Codice</th>"
      +"<th>Nome</th>"
      +"<th>Descrizione</th>"
      +"<th>Modifica</th>"
      +"<th>Elimina</th>"
      +"</tr>";
    var indCurrenct=0;
    while(indCurrenct<numItem)
    {
      //                    alert("idn " + ind);
      if(ind<listaStudenti.length)
      {
        tmp=listaStudenti[ind].split("*");

//            tmp[1]=tmp[1].replace("'", "\"");
//            alert("tmp[1]: " +tmp[1]);
//            tmp[2]=tmp[2].replace("'", "\'");
        str+='<input type="hidden" id="codArg'+tmp[0]+'" value="'+tmp[0]+'"/><tr><td>'+tmp[0]+'</td>'+
          '<td><input type="text" id="nomeArg'+tmp[0]+'" value="'+tmp[1]+'" maxlength="30" /></td>'+
          '<td><input type="text" id="descrizioneArg'+tmp[0]+'" value="'+tmp[2]+'" maxlength="100"/></td>'+
          '<td><a href="javascript:modifica('+tmp[0]+')">Modifica</a></td>'+
          '<td><a href="javascript:elimina('+tmp[0]+')">Elimina</a></td>';
      }
      ind++;
      indCurrenct++;
    }
    str +="</table>";
    var numTotItem = listaStudenti.length;
    var numPag = Math.ceil(numTotItem / numItem);
    if(numPag > 1)
    {
      str +="<div id='navigator'><center>";
      var current = 0;
      var toPrint=1
      for(k=1;k<=numPag;k++)
      {
        str+="<a href='javascript:manageNav("+current+")'>"+toPrint+"</a>&nbsp;&nbsp;";
        current= k * numItem;
        toPrint = toPrint+1;
      }
      str+="</center></div>";
    }
    document.getElementById('resultsCont').innerHTML=str;
  }

  function modifica(cod)
  {
    document.getElementById('codiceModArg').value=document.getElementById('codArg'+cod).value;
    document.getElementById('nomeModArg').value=document.getElementById('nomeArg'+cod).value;;
    document.getElementById('descModArg').value=document.getElementById('descrizioneArg'+cod).value;;

    document.getElementById('modificaForm').submit();

  }

  function elimina(cod)
  {
    var listaArg = new Array(<?= count($argomentiFromQuestion) ?>);
    var codiceArg = document.getElementById('codArg'+cod).value;
    var ind = 0;
<? for ($i = 0; $i < count($argomentiFromQuestion); $i++) { ?>
              listaArg[ind]=<?= $argomentiFromQuestion[$i] ?>;
              ind++;

<? } ?>

            var flag=false;
            for(i=0;i<listaArg.length;i++)
              if(codiceArg == listaArg[i])
                flag=true;

            if(flag==true)
              location.href='../portal/manageErrors.php?lista=26*';
            else
            {
              document.getElementById('codiceDelete').value=document.getElementById('codArg'+cod).value;
              document.getElementById('deleteForm').submit();
            }

          }

          function checkLenDesc()
          {
            var rest;
            max=100;
            var len = document.getElementById('descArgomento').value.length;

            if (len > max) {
              document.getElementById('descArgomento').value = document.getElementById('descArgomento').value.substring(0,max)
              rest = 0
            }
            else {
              rest = max - len
            }

            document.getElementById('numCaratteriRimanenti').innerHTML = rest;

          }

          function checkForm()
          {
            var nome = document.getElementById('nomeArg').value;
            var desc = document.getElementById('descArgomento').value;

            var strErr="";
            if(nome.length ==0)
              strErr += "21*";
//            if(desc.length ==0)
//              strErr += "22*";

            if(strErr.length>0)
            {
              location.href='../portal/manageErrors.php?lista='+strErr;
              return false;
            }

            return true;
          }
    </script>

  </head>
  <body>
    <form id="modificaForm" method="POST" action="argomentoListener.php">
      <input type="hidden" name="operation" value="update"/>
      <input type="hidden" id="codiceModArg"name="codice" value=""/>
      <input type="hidden" id="nomeModArg" name="nome" value=""/>
      <input type="hidden" id="descModArg" name="descrizione" value=""/>

    </form>

    <form id="deleteForm" method="POST" action="argomentoListener.php">
      <input type="hidden" name="operation" value="delete"/>
      <input type="hidden" name="codice" id="codiceDelete" value=""/>
    </form>
<?php include '../portal/header.php'; ?>
    <div id="detail"></div>
    <div id="container">

      <div id="container2">
        <div id="header">
          <center>
            <h3><center>Nuovo Argomento</center></h3>
          </center>
        </div>
        <div id="navigation">
          <ul>
            <li><a href="../portal/newMenu.php"><center><img src="../images/icons/64/home.png" height="40" width="40"  border="0" title="Home"/></center></a></li>
          </ul>
        </div>
        <div id="extra">
          Questa sezione ti permette di inserire un nuovo argomento per la tua disciplina, usato nel comporre una domanda
        </div>
        <div id="content">
          <center>
            <form name="formUser" method="POST" action="argomentoListener.php" onsubmit="return checkForm()">
              <input type="hidden" name="operation" value="insert"/>
              <input type="hidden" name="codice" value="<?php echo $result->getCodice(); ?>" />
              <input type="hidden" name="codiceDisciplina" value="<?php echo $result->getDisciplina(); ?>" />
              <table width="80%" cellspacing="13" cellpadding="4" style="font-size: 12px;">
                <tr>
                  <td colspan="2"><strong>Nome:</strong> </td>
                  <td><input type="text" name="nome" id="nomeArg" maxlength="30" /></td>
                </tr>
                <tr>
                  <td colspan="2"><strong>Descrizione:</strong> </td>
                  <td><textarea cols="25" rows="4" name="descrizione" id="descArgomento" onkeyup="javascript:checkLenDesc();"></textarea> </td>

                  <td><b>Caratteri rimanenti descrizione :</b>&nbsp;<div style="text-align: center;"id="numCaratteriRimanenti"><script type="text/javascript">document.write(rimanenti);</script></div></td>

                </tr>
                <tr>
                  <td colspan="2"><strong>Disciplina:</strong> </td>
                  <td>
                    <select name="codiceDisciplina" id="disciplina">
<?
for ($i = 0; $i < count($disciplineUtente); $i++) {
  $disciplinaDesc = DisciplinaDataManager::getInstance()->getDisciplina($connection, $disciplineUtente[$i]);

  echo '<option value=' . $disciplinaDesc->getCodice() . '>' . $disciplinaDesc->getNome() . '</option>';
}
?>
                    </select>
                  </td>
                </tr>


                <tr>
                  <td colspan="2">&nbsp;</td>
                  <td><input type="submit" value="Inserisci"   /></td>

                </tr>
              </table>

              <div id="resultsCont"></div>
              <script>manageNav(0);</script>



            </form>


            <br><br><br><br><br><br><br><br><br><br>
          </center>
        </div>

        <div id="footer"><strong>© Info test on line</strong></div>

      </div>
    </div>

  </body>
</html>
